using System.Linq;
using Admin3000.Backend.Api.Entity;
using Admin3000.Backend.Api.Params;
using Admin3000.Backend.Api.Repository;
using Admin3000.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Admin3000.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class ArticlesController : ControllerBase
    {
        private IConfiguration _configuration;

        private IRepository<Articles> _articlesRepository;

        private TokenParameter _tokenParameter;

        public ArticlesController(
            IConfiguration configuration,
            IRepository<Articles> articlesRepository
        )
        {
            _configuration = configuration;
            _articlesRepository = articlesRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }
        
        //id搜索
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var article = _articlesRepository.GetById(id);
            article.NumReadings+=1;
            return JsonHelper.Serialize    ( new
            {
                Code = 1000,
                Data = article,
                Msg = "获取成功"
            });
        }
        //列出文章列表
        public dynamic Ger()
        {
            var article=_articlesRepository.Table.ToList();

            
            return new{
                Code=1000,
                Data=new{Data=article},
                Msg="列出文章列表成功"
            };
        }


        [HttpGet("numReadings"),Route("numReadings")]
        //搜索阅读排行榜前5条
        public dynamic Getread()
        {
            var article=_articlesRepository.Table.ToList()
            .OrderByDescending(s => s.NumReadings).Take(5);
            return new{
            Code=1000,
            Data=new{Data=article},
            Msg="列出阅读列表成功"
        };

        }
        
        [HttpGet("latest"),Route("latest")]
        //搜索最新5条
        public dynamic latest()
        {
            var article=_articlesRepository.Table.ToList()
            .OrderByDescending(s => s.Id).Take(5);
            return new{
            Code=1000,
            Data=new{Data=article},
            Msg="列出最新列表成功"
        };
        }
        [HttpGet("addLikes/{id}"),Route("addLikes")]
        public dynamic addLikes(int id)
        {
            var article = _articlesRepository.GetById(id);
            article.Likes += 1;
            return JsonHelper.Serialize    ( new
            {
                Code = 1000,
                Data = article,
                Msg = "获取成功"
            });
        }
        [HttpGet("cancleLikes/{id}"),Route("cancleLikes")]
        public dynamic cancleLikes(int id)
        {
            var article = _articlesRepository.GetById(id);
            article.Likes -= 1;
            return JsonHelper.Serialize    ( new
            {
                Code = 1000,
                Data = article,
                Msg = "获取成功"
            });
        }


        
        //新增文章 标题、内容、点赞数、阅读数
        [HttpPost]
        public dynamic Post( CreateArticle newArticles)
        {
            var title=newArticles.Title;
            var context=newArticles.Context;
            var Avatar=newArticles.Avatar;
            var Author=newArticles.Author;
            var likes=newArticles.Likes==0;
            var numreadings=newArticles.NumReadings==0;
            

            var articles = new Articles{
                Title = newArticles.Title,
                Context = newArticles.Context,
                Likes=newArticles.Likes,
                NumReadings=newArticles.NumReadings,
                Avatar=newArticles.Avatar,
                Author=newArticles.Author
                
            };
            _articlesRepository.Insert(articles);
            return new{
                Code = 1000,
                Data=articles,
                Msg = "新增文章成功"
            };
        }


        //删除对应Id文章
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _articlesRepository.Delete(id);
            return new { Code = 1000, Data = "", Msg = "删除文章成功^_^" };
        }   

        //获取文章数量
        [HttpGet("homepage"),Route("homepage")]
        public dynamic homepage()
        {
            var arictles = _articlesRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = arictles },
                Msg = "获取文章列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }

        //更新阅读数量
        [HttpPut("{id}")]
        public dynamic Post(int id,CreateArticle createarticle )
        {
            var article=_articlesRepository.GetById(id);

            article.NumReadings=createarticle.NumReadings;

            _articlesRepository.Update(article);

            return new {
                Code=1000,
                Data=article,
                Msg="更新成功"
            };
        }
        [HttpGet("Likes"),Route("Likes")]
        //搜索阅读排行榜前5条
        public dynamic GetLikes()
        {
            var article=_articlesRepository.Table.ToList()
            .OrderByDescending(s => s.Likes).Take(5);
            return new{
            Code=1000,
            Data=new{Data=article},
            Msg="列出阅读列表成功"
        };
        }

    }
}
